//
//  DetailEdit.swift
//  Speek
//
//  Created by MikeHo on 2023/6/25.
//

import SwiftUI

struct DetailEdit: View {
    
    @ObservedObject private var EditingBill : BillDetail
    @State private var name: String = ""
    @State private var type: String = ""
    @State private var date: Date = .init()
    @State private var cost: Double = 0.0
    @State private var tag: Bool = false
    
    //初始化
    init(EditingBill: BillDetail) {
        self.EditingBill = EditingBill
    }
    
    // View Properties
    let incomeType :[String] = ["工资","奖学金","礼物","理财","其他"]
    let outputType :[String] = ["学习","医疗","饮食","维修","旅行","运动","娱乐","购物","理财","其他"]
    
    // Emvironment Properties
    @Environment(\.dismiss) private var dismiss
    @Environment(\.managedObjectContext) private var context

    var body: some View {
        
        NavigationStack{
            
            //更改即保存
            List{
                Section("交易类型"){
                    Button(action:{
                        tag.toggle()
                    },
                           label: {
                        Text(tag ? "收入":"支出")
                    })
                }
                //在更改账单的支出收入类型时
                //将账单的类型进行更改
                //以免出现类型与标签不符合的情况
                .onChange(of: tag) { newValue in
                    if tag{
                        type = incomeType[0]
                    }
                    else{
                        type = outputType[0]
                    }
                }
                
                Section("事件"){
                    TextField(name,text: $name)
                }
                
                Section("日期"){
                    DatePicker("",selection: $date,displayedComponents: [.date])
                        .labelsHidden()
                        .onAppear {
                            if EditingBill.date == nil {
                                EditingBill.date = Date()
                            }
                        }
                }
                
                Section("金额"){
                    TextField(value:$cost,formatter: currencyFormatter){}
                        .labelsHidden()
                        .keyboardType(.decimalPad)
                }
                
                Section("种类"){
                    Picker(selection: $type,
                           label: Image(type)
                        .padding(.leading,5)){
                            ForEach(tag ? incomeType:outputType,id:\.self){type in
                                Text(type)
                            }
                        }
                        .pickerStyle(DefaultPickerStyle())
                }
            }
            .preferredColorScheme(.dark)
            .navigationTitle("编辑账单")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar{
                ToolbarItem(placement: .navigationBarTrailing){
                    Button("完成"){
                        saveChanges()
                    }
                }
            }
            .onAppear{
                date = EditingBill.date ?? Date()
                type = EditingBill.type ?? ""
                name = EditingBill.name ?? ""
                tag  = EditingBill.tag
                cost = EditingBill.cost
            }
        }
        
    }
    
    private func saveChanges() {
        do {
            EditingBill.cost = cost
            EditingBill.date = date
            EditingBill.name = name
            EditingBill.tag = tag
            EditingBill.type = type
            try context.save()
            dismiss()
        } catch {
            print("Error saving changes: \(error)")
        }
    }
}
